<?php if (!defined('__TYPECHO_ROOT_DIR__')) exit; ?>

<?php
 $GLOBALS['isLogin'] = $this->user->hasLogin();
 $GLOBALS['rememberEmail'] = $this->remember('mail',true);
function threadedComments($comments, $options)
{
    $commentClass = '';
    if ($comments->authorId) {
        if ($comments->authorId == $comments->ownerId) {
            $commentClass .= ' comment-by-author';
        } else {
            $commentClass .= ' comment-by-user';
        }
    }
    $commentLevelClass = $comments->levels > 0 ? ' comment-child' : ' comment-parent';
    if ($comments->url) {
        $author = '<a href="' . $comments->url . '"' . '" target="_blank"' . ' rel="external nofollow">' . $comments->author . '</a>';
    } else {
        $author = $comments->author;
    }
    ?>
    <li id="li-<?php $comments->theId(); ?>" class="comment-body<?php
                                                                if ($comments->levels > 0) {
                                                                    echo ' comment-child';
                                                                    $comments->levelsAlt(' comment-level-odd', ' comment-level-even');
                                                                } else {
                                                                    echo ' comment-parent';
                                                                }
                                                                $comments->alt(' comment-odd', ' comment-even');
                                                                echo $commentClass;
                                                                ?>">
        <div id="<?php $comments->theId(); ?>">
            
            <div class="comment-avatar">
                <img width="28" height="28" class="avatar-img" src="<?php _getAvatarLazyload() ?>" data-src="<?php _getAvatarByMail($comments->mail); ?>" alt="头像" />
                <span class="comment-author"><?php echo $author ?></span>
            </div>
        
            <div class="comment-content">
                <div class="comment-text">
                    <?php $comments->content();?>
                </div>
                
                <div class="comment-footer">
                    <span class="comment-time"><?php echo formatTime($comments->created); ?></span>
                    <button onclick="return TypechoComment.reply('comment-<?php $comments->coid(); ?>', <?php $comments->coid(); ?>);">回复</button>
                </div>
            </div>
            

        </div>
        <?php if ($comments->children) { ?><div class="comment-children"><?php $comments->threadedComments($options); ?></div><?php } ?>
    </li>
<?php } ?>

<div id="comments" class="comments-main">
    <?php $this->comments()->to($comments); ?>
    <?php if ($this->allow('comment')) : ?>
        <div id="<?php $this->respondId(); ?>" class="respond">
            <form method="post" action="<?php $this->commentUrl() ?>" id="comment-form" role="form" class="comment-form">
    <div class="comment-container">
        <div class="comment-main">
            <div class="comment-fields">
                <?php if ($this->user->hasLogin()) : ?>
                    <div class="admin-info">尊敬的站长<a class="admin-link" href="<?php $this->options->profileUrl(); ?>"><?php $this->user->screenName(); ?></a>, 欢迎回来! <a href="<?php $this->options->logoutUrl(); ?>" title="Logout"><?php _e('退出'); ?> &raquo;</a></div>
                <?php else : ?>
                    <div class="field-row">
                        <div class="field-group"><input type="text" name="author" id="comment-name" class="form-input" placeholder="<?php _e('名字'); ?>" value="<?php $this->remember('author'); ?>" required /></div>
                        <div class="field-group"><input type="email" name="mail" id="comment-mail" class="form-input" placeholder="<?php _e('邮箱'); ?>" value="<?php $this->remember('mail'); ?>" <?php if ($this->options->commentsRequireMail) : ?> required<?php endif; ?> /></div>
                        <div class="field-group"><input type="url" name="url" id="comment-url" class="form-input" placeholder="<?php _e('网址'); ?>" value="<?php $this->remember('url'); ?>" <?php if ($this->options->commentsRequireURL) : ?> required<?php endif; ?> /></div>
                    </div>
                <?php endif; ?>
            </div>

            <div class="comment-editor">
                <textarea name="text" id="textarea" placeholder="撰写评论..." class="comment-textarea" required onkeydown="if((event.ctrlKey||event.metaKey)&&event.keyCode==13){document.getElementById('submitComment').click();return false};"><?php $this->remember('text'); ?></textarea>
                
                <div class="comment-footer">
                        <button id="cancel-comment-reply-link" style="display:none" onclick="return TypechoComment.cancelReply();">取消回复</button>
                        <button id="submitComment" type="submit" class="submit-btn">发表评论</button>
                </div>
            </div>
        </div>
    </div>
</form>
        </div>
    <?php else : ?>
            <div class="comments_off">
                <svg xmlns="http://www.w3.org/2000/svg" width="1.5em" height="1.5em" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-lock-keyhole-icon lucide-lock-keyhole"><circle cx="12" cy="16" r="1"/><rect x="3" y="10" width="18" height="12" rx="2"/><path d="M7 10V7a5 5 0 0 1 10 0v3"/></svg>
                <?php _e('评论已关闭'); ?>
            </div>
    <?php endif; ?>
    <div class="comments_lie">
    <?php if ($comments->have()) : ?>
        <h3><?php $this->commentsNum(_t('暂无评论'), _t('仅有一条评论'), _t('已有 %d 条评论')); ?></h3>
        <?php $comments->listComments(); ?>
        <div class="paging">
            <?php $comments->pageNav('', ''); ?>
            <!-- 修改评论样式 -->
            <?php
                // 获取当前页面的文章 CID
                $cid = $this->cid;
                // 查询当前页面的评论总数
                // 查询当前页面的评论总数，只查询 parent 为 0 的评论
                $comment_count = $this->db->fetchRow($this->db->select('COUNT(*) AS count')->from('table.comments')
                    ->where('cid = ?', $cid)
                    ->where('status = ?', 'approved')
                    ->where('parent = ?', 0))['count'];//只查询父级评论，否则会导致分页存在问题
                    
                if($comment_count > 6)://注意 这里的数字一定要与主题设置中分页显示的评论数一致，换言之 如果评论数有下一页 则显示加载更多按钮?>
                
                <!-- 添加一个按钮用来加载下一页评论 -->
                <div id="loading-spinner">
                    <div class="spinner"></div>
                    <span>加载中...</span>
                </div>
                <?php endif; ?>
                <!-- 添加一个用于显示提示信息的元素 -->
                <div class="end">
                    <div id="no-more" style="display: none;">END</div>
                </div>
            <?php endif; ?>
        </div>
    </div>
</div>

